/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.smart.hawk.model.caller;

<<<<<<< HEAD
import co.com.smart.hawk.dto.ArticulosDTO;
=======
import co.com.smart.connection.model.manager.JDBCResourceManager;
>>>>>>> hawk_menu
import co.com.smart.hawk.dto.InventarioInicialDTO;
import co.com.smart.hawk.dto.SitiosDTO;
import co.com.smart.hawk.dto.UsuarioDTO;
import co.com.smart.hawk.utilities.HawkSessionMgr;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ceyni Paola
 */
public class InventarioInicialcaller extends JDBCResourceManager{

    public InventarioInicialcaller() {
        super();
    }
    
    /**
     * Almacena los articulos
     * @param inventario
     * @return
     * @throws Throwable 
     */
    
    public InventarioInicialDTO grabarInventarioInicial(InventarioInicialDTO inventario)throws Throwable{
    InventarioInicialDTO returnInventario = new InventarioInicialDTO();
    
    Connection conn = null;
        CallableStatement call = null;
        UsuarioDTO user = (UsuarioDTO)HawkSessionMgr.getInstance().getSession("USER_SESSION");
        try{
        conn = getConnection();
        call = conn.prepareCall("{call GrabarInventario(?,?,?,?,?,?,?)}");
        if(inventario.getScinventario() != null){
            call.setBigDecimal(1, inventario.getScinventario());
        }else{
            call.setInt(1, java.sql.Types.NULL);
        }
        call.setBigDecimal(2,inventario.getArticulos().getScarticulo());
        call.setBigDecimal(3,inventario.getSitios().getScsitios());
        call.setDouble(4,inventario.getNmcantidad());
        call.setString(5,user.getCocodigo());
        call.registerOutParameter(6, java.sql.Types.VARCHAR);
        call.registerOutParameter(7, java.sql.Types.VARCHAR);
        call.executeUpdate();
        returnInventario.setDsmensaje(call.getString(6));
        returnInventario.setCodigo(call.getString(7));
        
        
        
        }catch (SQLException e) {
            throw  e;
        }finally{
            closeResources(conn, call);
        }
        
        return returnInventario;
        
    }
    
       /**
     * Consulta los datos maestros apartir del maerstro filtrado
     * @param filtros
     * @return 
     */
    public List<InventarioInicialDTO> listarInventarioinicial(InventarioInicialDTO filtros) throws Throwable {
        List<InventarioInicialDTO> listinventario = new ArrayList<>();
        Connection conn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try{
            conn = getConnection();
            call = conn.prepareCall("{call consultarInventario()}");
           // call.setString(1, filtros.getCoempresa());
            rs = call.executeQuery();
            
            if (rs!=null){
                while(rs.next()){
                    InventarioInicialDTO inventarioInicialDTO = new InventarioInicialDTO();
                    ArticulosDTO articulosDTO = new ArticulosDTO();
                    SitiosDTO sitiosDTO = new SitiosDTO();
                    
                    inventarioInicialDTO.setScinventario(rs.getBigDecimal("sm_scinventario"));
                    articulosDTO.setScarticulo(rs.getBigDecimal("sm_scarticulo"));
                    articulosDTO.setDsarticulo(rs.getString("sm_dsarticulo"));
                    inventarioInicialDTO.setArticulos(articulosDTO);
                    sitiosDTO.setScsitios(rs.getBigDecimal("sm_scsitios"));
                    sitiosDTO.setDssitios(rs.getString("sm_dssitios"));
                    inventarioInicialDTO.setSitios(sitiosDTO);
                    inventarioInicialDTO.setNmcantidad(rs.getDouble("sm_nmcantidad"));
                    inventarioInicialDTO.setCousuario(rs.getString("sm_cousuario"));

                    listinventario.add(inventarioInicialDTO);
                }
            }
        } catch (SQLException e) {
            throw  e;
        }finally{
            closeResources(conn, call, rs);
        }

        return listinventario;

    }
}
